<template>
  <!-- 模块1 -->
  <div class="modular">
    <div class="modular_top">
      <div class="triangle"></div>
      <div class="modular_icon">
        <img src="@/assets/img/icon1.png" alt="">
      </div>
      <h2>泵房信息</h2>
    </div>
    <div class="house_cont">
      <div class="house_left">
        <div class="house_left_icon" @click="getHouse">
          <img v-show="siteId == null" src="@/assets/img/YT.gif" alt="">
          <img v-show="siteId == 1" src="@/assets/img/YT.gif" alt="">
          <img v-show="siteId == 2" src="@/assets/img/XHW.gif" alt="">
        </div>
      </div>
      <div class="house_right">
        <div class="title">
          <h2>{{ this.data.site_name }} <span>(YT000001)</span></h2>
          <div class="use">
            <h3>使用中</h3>
          </div>
        </div>
        <div class="position">
          <img src="@/assets/img/position.png" alt="">
          <span>{{ this.data.install_address }}</span>
        </div>
        <div class="line"></div>
        <div class="house_text">
          <div class="items">
            <!-- <div class="item">泵房类型 ：<span>{{ this.data.site_type }}</span></div> -->
            <div class="item"><span class="max">负责人</span> ：<span>{{ this.data.adminuser }}</span></div>
            <div class="item">泵房面积 ：<span>{{ this.data.acreage }}m3</span></div>
          </div>
          <div class="items">
            <div class="item">厂家类型 ：<span>{{ this.data.manufactor_type }}</span></div>
            <div class="item">投运时间 ：<span>{{ this.data.built_date }}</span></div>
          </div>
          <div class="items">
            <div class="item">设备类型 ：<span>{{ this.data.device_type }}</span></div>
            <div class="item">联系方式 ：<span>{{ this.data.adminphone }}</span></div>
          </div>
        </div>
        <div class="echarts_time"></div>
      </div>
    </div>
  </div>
</template>
<script>
import * as api from '@/assets/utils/api.js'
export default {
  data() {
    return {
      myChart: {},
      data: [],
      siteId: null,
      siteNo: null,
    }
  },
  // async created() {
  //   this.getData(this.siteId, this.siteNo)
  // },
  mounted() {
    this.getData(this.siteId, this.siteNo)
    window.addEventListener('resize', () => {
      this.myChart.resize()
    })
  },
  methods: {
    getHouse() {
      this.$emit('onChange', false)
    },
    changeData(value, value2) {
      this.siteId = Number(value)
      this.siteNo = Number(value2)
      // console.log(value)
      this.getData(this.siteId, this.siteNo)
    },
    async getData(idNum, idNum2) {
      // 接口参数
      const id = { tenantid: 1128, siteId: idNum }
      const res = await api.house(id)
      this.data = res.data[0]
      this.data.built_date = this.transformTimestamp(this.data.built_date)
      const id2 = { tenantid: 1128, siteId: idNum, siteNo: idNum2 }
      const res2 = await api.waterCntCurve(id2)
      this.drawLine(res2.data)
    },
    transformTimestamp(timestamp) {
      let a = new Date(timestamp).getTime()
      const date = new Date(a)
      const Y = date.getFullYear() + '-'
      const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
      const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + '  '
      // const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
      // const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
      // const s = date.getSeconds(); // 秒
      // const dateString = Y + M + D + h + m
      const dateString = Y + M + D
      // console.log('dateString', dateString); // > dateString 2021-07-06 14:23
      return dateString
    },
    drawLine(data) {
      // 基于准备好的dom，初始化echarts实例
      this.myChart = this.$echarts.init(document.querySelector('.echarts_time'))
      this.myChart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'line'
          },
          // extraCssText: 'background: #023881;border:none;color:#fff;opacity: 0.9',
          // formatter(params) {
          //   let result = params[0].axisValue
          //   params.forEach(item => {
          //     // result += '<br/>' + params[i].marker + params[i].value + '%'
          //   })
          // }
        },
        xAxis: {
          type: 'category',
          axisTick: {
            show: false // 不显示坐标轴刻度线
          },
          axisLabel: {
            textStyle: {
              color: '#50D5FD'
            }
          },
          axisLine: {
            lineStyle: { color: '#00CCFF' }  //x轴坐标轴颜色
          },
          data: data.fd_datas
        },
        yAxis: {
          type: 'value',
          show: true,
          splitNumber: 3,
          splitLine: {
            //网格线
            show: false,
            // lineStyle: {
            //   color: '#00CCFF'
            // }
          },
          axisLabel: {
            show: true,
            textStyle: {
              color: '#50D5FD'
            }
          },
          axisLine: {
            show: true,
            lineStyle: { color: '#00CCFF' }  //x轴坐标轴颜色
          },
          axisTick: {
            show: true,
            inside: true,
            length: 3
          },
        },
        legend: {
          data: ['供水量', '耗电量', '吨水电耗'],
          top: '0%',
          itemHeight: 5,
          itemWidth: 8,
          textStyle: {
            color: '#fff',
            fontSize: '0.1rem'
          }
        },
        grid: {
          top: '30%',
          left: '0%',
          bottom: '0%',
          right: '0%',
          // height: '85%',
          containLabel: true
        },
        series: [
          {
            name: '供水量',
            data: data.water_cnt,
            type: 'line',
            smooth: true,
            showSymbol: false
          },
          {
            name: '耗电量',
            data: data.elec_cnt,
            type: 'line',
            smooth: true,
            showSymbol: false
          },
          {
            name: '吨水电耗',
            data: data.per,
            type: 'line',
            smooth: true,
            showSymbol: false
          }
        ]
      })
      window.addEventListener('resize', () => {
        this.myChart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.modular {
  .modular_top {
    left: -0.8%;
  }

  .house_cont {
    display: flex;
    justify-content: space-between;
    padding: .6rem .21rem 0 .21rem;

    .house_left {
      width: 41%;

      .house_left_icon {
        width: 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;

        img {
          width: 100%;
        }
      }
    }

    .house_right {
      width: 56%;
      // padding-bottom: .5rem;
      display: flex;
      flex-direction: column;
      position: relative;

      .title {
        display: flex;
        justify-content: space-between;

        h2 {
          color: #fff;
          font-size: 0.25rem;
          font-family: "Pang";
          letter-spacing: 0.055rem;
          white-space: nowrap;

          span {
            font-size: 0.18rem;
            letter-spacing: 0;
            margin-left: -5px;
          }
        }

        .use {
          width: .62rem;
          height: .2rem;
          background: rgba(23, 232, 255, 0.2);
          border: 1px solid #17E8FF;
          // border-image: linear-gradient(-27deg, #FF4C4B, #FF6A6B) 10 10;
          border-radius: 25px;
          margin-top: .1rem;

          h3 {
            font-size: 0.12rem;
            color: #17E8FF;
            text-align: center;
            line-height: .2rem;
          }
        }
      }

      .position {
        display: flex;

        img {
          width: 0.14rem;
          margin: 0 5px 0 8px;
        }

        span {
          font-size: .14rem;
          color: #CCDDFF;
        }
      }

      .line {
        width: 100%;
        height: 1px;
        background-color: #5fa1ff;
        opacity: 0.6;
        margin-top: 0.07rem;
        position: relative;

        &::after {
          content: "";
          width: 96%;
          height: 1px;
          background: #2b518a;
          position: absolute;
          left: 2%;
          top: 0;
        }
      }

      .house_text {
        display: flex;
        padding: 0.18rem 0 0 0.1rem;

        .items {
          width: 30.3%;

          &:nth-child(2) {
            width: 34.3%;
          }

          &:nth-child(3) {
            width: 35%;
          }

          .item {
            // width: 33.3%;
            display: flex;
            font-size: 0.12rem;
            color: #fff;
            margin-bottom: 0.1rem;
            white-space: nowrap;

            span {
              color: #17E8FF;
            }

            .max {
              color: #fff;
              letter-spacing: 0.05rem;
            }
          }
        }
      }

      .echarts_time {
        width: 100%;
        height: 0.85rem;
      }
    }
  }
}
</style>